package dao

import models.Tables._
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.jdbc.JdbcProfile

import javax.inject.{Inject, Singleton}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

@Singleton
class FreezerSetDao @Inject()(protected val dbConfigProvider: DatabaseConfigProvider) extends
  HasDatabaseConfigProvider[JdbcProfile] {

  import profile.api._

  val table = FreezerSet
  type MyRow = FreezerSetRow

  def update(row: MyRow): Future[Unit] = {
    db.run(table.filter(x => x.freezerNumber === row.freezerNumber && x.freezerLayerNumber === row.freezerLayerNumber).
      update(row)).map(_ => ())
  }

  def insertOrUpdate(row: MyRow) = {
    db.run(table.insertOrUpdate(row))
  }

  def selectOp(row: MyRow) = {
    db.run(table.filter(x => x.freezerNumber === row.freezerNumber && x.freezerLayerNumber === row.freezerLayerNumber)
      .result.headOption)
  }

}
